package com.hmall.service.imp;

import com.alibaba.fastjson.JSON;
import com.hmall.common.config.ItemFrignConfig;
import com.hmall.common.dto.Item;
import com.hmall.pojo.ItemDoc;
import com.hmall.service.HmallService;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
@Service
@Transactional
public class HmallServiceImpl implements HmallService {
    @Autowired
    private RestHighLevelClient client;
    @Autowired
    private ItemFrignConfig frignConfig;
    @Override
    public void insertMqById(Long id) {
        Item item1 = frignConfig.getByIdController(id);
        ItemDoc itemDoc = new ItemDoc(item1);
        IndexRequest request = new IndexRequest("hmall1").id(id.toString());
        request.source(JSON.toJSONString(itemDoc), XContentType.JSON);
        try {
            client.index(request,RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void deleteMqById(Long id) {
        DeleteRequest request = new DeleteRequest("hmall1", id.toString());
        try {
            client.delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
